<template>
    <n-card :bordered="false" class="proCard">
        <BasicTable :columns="columns" :request="loadDataTable" :row-key="(row: ListDataAboutApplyManagement) => row.id"
            ref="actionRef" :actionColumn="actionColumn" @update:checked-row-keys="onCheckedRow" :scroll-x="1090">
        </BasicTable>
    </n-card>
</template>

<script lang="ts" setup>
import { h, reactive, ref } from 'vue';
import { BasicTable, TableAction } from '@/components/Table';


import { getTableListAboutApplyApi, updateApplyStateApi } from '@/api/admin/arrangeManagement';
import { columnsAboutApplyManagement, ListDataAboutApplyManagement } from './columns';
import { useRouter } from 'vue-router';
import { useMessage } from 'naive-ui';


const columns = ref(columnsAboutApplyManagement);

const router = useRouter();
const message = useMessage();
const actionRef = ref();

const successOperation = 0
const failOperation = 1

const actionColumn = reactive({
    width: 220,
    title: '操作',
    key: 'action',
    fixed: 'right',
    render(record: any) {
        return h(TableAction as any, {
            style: 'NButton',
            actions: [
                {
                    label: '通过',
                    onClick: handlePermit.bind(null, record, successOperation),
                    type: 'info',
                    secondary:true,
                    ifShow: () => {
                        return isManage(record);
                    },

                },
                {
                    label: '未通过',
                    type: 'error',
                    secondary:true,
                    onClick: handlePermit.bind(null, record, failOperation),
                    ifShow: () => {
                        return isManage(record);
                    },

                },

            ],
        });
    },
});


function isManage(record: any) {
    return record.state == 0
}

var pageNum // 接口参数为oageNum 而 async参数res变量为page  //todo 改进
const loadDataTable = async (res: any) => {
    pageNum = res.page
    
    return await getTableListAboutApplyApi({ ...res, "pageNum": pageNum });

};

function onCheckedRow(rowKeys: any) {
    console.log(rowKeys);
}

function reloadTable() {
    actionRef.value.reload();
}

async function handlePermit(record: Recordable, operation: number) {
    await updateApplyStateApi({
        borrowId: record.id,
        state: operation + 1,
    })
    reloadTable();
}

</script>

<style lang="less" scoped></style>